<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Services\ToolsService;

class ToolsController extends Controller
{
    public function getClientInfo(Request $request)
    {
        $realClientIp = $request->getClientIp();
        // 获取客户端的 User-Agent
        $userAgent = $request->userAgent();

        $data = ToolsService::saveRequestInfo([
            'ip'         => $realClientIp,
            'user_agent' => $userAgent,
            'is_save'    => $request->get('is_save', 0),
        ]);

        return response()->json($data, 200, [], JSON_UNESCAPED_UNICODE)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * Gitee WebHook.
     *
     * @return void
     */
    public function giteeWebhook()
    {
        $payload = file_get_contents('php://input');
        ToolsService::giteeWebhook($payload);

        successJson(['time' => date('Y-m-d H:i:s'), 'payload' => $payload]);
    }
}
